package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.WorkprojectMapper;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.service.*;
import com.hbnu.system.utils.CheckRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class WorkprojectServiceImpl extends ServiceImpl<WorkprojectMapper, Workproject> implements IWorkprojectService {

    @Autowired
    ITranproject_registrationService iTranproject_registrationService;

    @Autowired
    IPorproject_registrationService iPorproject_registrationService;

    @Autowired
    IWorkService iWorkService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 新增项目
     * @param workproject 要新增的著作项目
     * @return 新增操作是否成功
     */
    @Override
    public boolean addWorkProject(Workproject workproject) {
        // 拦截普通用户在非法状态修改的操作
        Work work = iWorkService.getOne(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getWorkID, workproject.getWorkID()));
        checkRoleUtil.checkPermissionAchievement(work.getStatus());

        if(StrUtil.isEmpty(workproject.getProjectName()) || StrUtil.isBlank(workproject.getProjectName())){
            throw new BaseException("请输入项目名称!");
        }

        Porproject_registration porproject = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectName, workproject.getProjectName()));

        Tranproject_registration tranproject = iTranproject_registrationService.getOne(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectName, workproject.getProjectName()));

        if(Objects.nonNull(porproject)){
            workproject.setHead(porproject.getHead());
            workproject.setProjectID(porproject.getProjectId());
            if(Objects.isNull(workproject.getStartTime())){
                workproject.setStartTime(porproject.getStartTime());
            }
            if(Objects.isNull(workproject.getEndTime())){
                workproject.setEndTime(porproject.getEndTime());
            }
        } else if(Objects.nonNull(tranproject)){
            workproject.setHead(tranproject.getHead());
            workproject.setProjectID(tranproject.getProjectId());
            if(Objects.isNull(workproject.getStartTime())){
                workproject.setStartTime(tranproject.getStartTime());
            }
            if(Objects.isNull(workproject.getEndTime())){
                workproject.setEndTime(tranproject.getEndTime());
            }
        } else {
            throw new BaseException("项目不存在!");
        }

        // 判断该项目是否已经添加过在该著作中
        List<Workproject> workprojectList = query().eq("projectName", workproject.getProjectName())
                .eq("WorkID", workproject.getWorkID()).list();
        if(!workprojectList.isEmpty()){
            throw new BaseException("该项目已添加过!");
        }

        return baseMapper.insert(workproject) == 1;
    }

    /**
     * 删除著作项目
     * @param workProjectID 要删除的著作项目 ID
     * @return 删除操作是否成功
     */
    @Override
    public boolean removeworkProject(String workProjectID) {
        Workproject workproject = super.getOne(Wrappers.lambdaQuery(Workproject.class)
                .eq(Workproject::getWorkProjectID, workProjectID));
        if(Objects.isNull(workproject)){
            throw new BaseException("不存在该关联项目");
        }
        // 拦截普通用户在非法状态修改的操作
        Work work = iWorkService.getOne(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getWorkID, workproject.getWorkID()));
        checkRoleUtil.checkPermissionAchievement(work.getStatus());

        return baseMapper.deleteById(workproject) == 1;
    }

    /**
     * 修改著作项目
     * @param workproject 要修改的著作项目
     * @return 修改操作是否成功
     */
    @Transactional
    @Override
    public boolean updateWorkProject(Workproject workproject) {
        Work work = iWorkService.getOne(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getWorkID, workproject.getWorkID()));
        checkRoleUtil.checkPermissionProject(work.getStatus());
        // 删除之前的项目
        removeById(workproject.getWorkProjectID());
        // 新增当前项目
        addWorkProject(workproject);
        return true;
    }

    /**
     * 根据著作编号查询
     * @param workID 著作编号
     * @return 符合条件的著作项目列表
     */
    @Override
    public List<Workproject> getWorkProjectByWorkID(String workID) {
        Work byId = iWorkService.getById(workID);
        if (Objects.isNull(byId)){
            throw new BaseException("输入的著作编号有误!");
        }
        List<Workproject> workprojects = baseMapper.selectList(Wrappers.lambdaQuery(Workproject.class)
                .eq(Workproject::getWorkID, workID));
        // 将 head 置的 id 转为名称
        for (Workproject workproject : workprojects) {
            String head = workproject.getHead();
            User user = iUserService.getById(head);
            workproject.setHead(user.getName());
        }
        return workprojects;
    }

}